package com.ruoyi.web.controller.cems;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cems.domain.dto.BmsWorkChecklistDTO;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cems.domain.BmsWorkChecklists;
import com.ruoyi.cems.service.IBmsWorkChecklistsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作检查单Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@Api(description = "工作检查单")
@RestController
@RequestMapping("/cems/workChecklists")
public class BmsWorkChecklistsController extends BaseController
{
    @Autowired
    private IBmsWorkChecklistsService bmsWorkChecklistsService;

    /**
     * 查询工作检查单列表
     */
    @ApiOperation(value = "查询工作检查单列表")
    @GetMapping("/list")
    public TableDataInfo list(BmsWorkChecklists bmsWorkChecklists)
    {
        startPage();
        if(!SecurityUtils.hasRole("manager")){
            //数据权限:普通用户：仅自己
            bmsWorkChecklists.setCheckPeopleId(getUserId().toString());
        }
        bmsWorkChecklists.setUnitCode(getLoginUser().getUser().getUnit());
        List<BmsWorkChecklists> list = bmsWorkChecklistsService.selectBmsWorkChecklistsList(bmsWorkChecklists);
        return getDataTable(list);
    }

    /**
     * 导出工作检查单列表
     */
    @ApiOperation(value = "导出工作检查单列表")
    @Log(title = "工作检查单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmsWorkChecklists bmsWorkChecklists)
    {
        bmsWorkChecklists.setUnitCode(getLoginUser().getUser().getUnit());
        List<BmsWorkChecklists> list = bmsWorkChecklistsService.selectBmsWorkChecklistsList(bmsWorkChecklists);
        ExcelUtil<BmsWorkChecklists> util = new ExcelUtil<BmsWorkChecklists>(BmsWorkChecklists.class);
        util.exportExcel(response, list, "工作检查单数据");
    }

    /**
     * 获取工作检查单详细信息
     */
    @ApiOperation(value = "获取工作检查单详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(bmsWorkChecklistsService.selectBmsWorkChecklistsByID(ID));
    }

    @ApiOperation(value = "转移检查单")
    @Log(title = "转移检查单", businessType = BusinessType.UPDATE)
    @PutMapping("/transfer")
    public AjaxResult transfer(@RequestBody BmsWorkChecklistDTO bmsWorkChecklistDTO) {
        return success(bmsWorkChecklistsService.transfer(bmsWorkChecklistDTO.getNoticeId(), bmsWorkChecklistDTO.getChecklistId(), bmsWorkChecklistDTO.getUserId()));
    }

    /**
     * 新增工作检查单
     */
    @ApiOperation(value = "新增工作检查单")
    @Log(title = "工作检查单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsWorkChecklists bmsWorkChecklists)
    {
        bmsWorkChecklists.setID(UUID.randomUUID().toString().replace("-", ""));
        bmsWorkChecklists.setCreatedBy(getUserId().toString());
        bmsWorkChecklists.setCreatedTime(new Date());
        bmsWorkChecklists.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(bmsWorkChecklistsService.insertBmsWorkChecklists(bmsWorkChecklists));
    }

    /**
     * 修改工作检查单
     */
    @ApiOperation(value = "修改工作检查单")
    @Log(title = "工作检查单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsWorkChecklists bmsWorkChecklists)
    {
        bmsWorkChecklists.setUpdatedBy(getUserId().toString());
        bmsWorkChecklists.setUpdatedTime(new Date());
        return toAjax(bmsWorkChecklistsService.updateBmsWorkChecklists(bmsWorkChecklists));
    }

    @ApiOperation(value = "已到岗")
    @Log(title = "已到岗", businessType = BusinessType.UPDATE)
    @PutMapping("/reportToDuty")
    public AjaxResult reportToDuty(@RequestBody BmsWorkChecklists bmsWorkChecklists)
    {
        String checkId = bmsWorkChecklists.getCheckPeopleId();
        Long  userId= getUserId();
        if (checkId != null && userId != null) {
            // 将 Long 类型的 userId 转换为 String 类型
            String userIdStr = userId.toString();

            if (checkId.equals(userIdStr)) {
                return toAjax(bmsWorkChecklistsService.reportToDuty(bmsWorkChecklists.getID()));
            } else {
                // 处理不相等的情况，例如抛出异常或返回错误信息
                throw new IllegalStateException("检查人ID和当前用户ID不匹配");
            }
        } else {
            // 处理空值的情况
            throw new IllegalStateException("检查人ID或当前用户ID为空");
        }

    }

    /**
     * 删除工作检查单
     */
    @ApiOperation(value = "删除工作检查单")
    @Log(title = "工作检查单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(bmsWorkChecklistsService.deleteBmsWorkChecklistsByIDs(IDs));
    }
    /**
     * 获取填报的工作检查单
     */
    @ApiOperation(value = "获取填报的工作检查单")
    @GetMapping(value = "/getFilledWorkChecklistsById/{ID}")
    public AjaxResult getFilledWorkChecklistsById(@PathVariable("ID") String ID)
    {
        return success(bmsWorkChecklistsService.getFilledWorkChecklistsById(ID));
    }

    /**
     * 填报检查单
     */
    @ApiOperation(value = "填报检查单")
    @PostMapping(value = "/fillWorkCheckDetail")
    public AjaxResult fillWorkCheckDetail(@RequestBody BmsWorkChecklistDTO dto)
    {
        BmsWorkChecklists bmsWorkChecklists = bmsWorkChecklistsService.selectBmsWorkChecklistsByID(dto.getId());
        String checkId = bmsWorkChecklists.getCheckPeopleId();
        Long  userId= getUserId();
        if (checkId != null && userId != null) {
            // 将 Long 类型的 userId 转换为 String 类型
            String userIdStr = userId.toString();

            if (checkId.equals(userIdStr)) {
                dto.setFillPeople(getUserId().toString());
                return success(bmsWorkChecklistsService.fillWorkCheckDetail(dto));
            } else {
                // 处理不相等的情况，例如抛出异常或返回错误信息
                throw new IllegalStateException("任务执行人和当前用户不匹配");
            }
        } else {
            // 处理空值的情况
            throw new IllegalStateException("检查人ID或当前用户ID为空");
        }
    }
}
